package de.schlinke_marius.activities;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import de.schlinke_marius.R;
import de.schlinke_marius.adapters.ImageAdapter;
import de.schlinke_marius.data.FragmentConstants;
import de.schlinke_marius.database.MSCDatabase;
import de.schlinke_marius.dialogs.BigPictureDialog;
import de.schlinke_marius.views.DiagramCanvas;

public class ExamplesDetail extends SherlockFragmentActivity implements FragmentConstants{

	final static String TAG = ExamplesDetail.class.getSimpleName();
	private Gallery gallery = null;


	private String date = null;
	private String hardwareCosts = null;
	private String softwareCosts = null;
	private String consultingCosts = null;
	private String extracosts = null;
	private String cpuBenchmark = null;
	private String graphicBenchmark = null;

	private MSCDatabase database = new MSCDatabase(this);
	private Cursor exampleDetailCursor = null;
	private Cursor hardwareCursor = null;
	private Cursor benchmarkCursor = null;
	private Cursor galleryPicturesCursor = null;
	private int selectedPicture;


	/*
	 * UI elements
	 */
	private TextView tvDate = null;
	private TextView tvDescription = null;
	private TextView tvProcessor = null;
	private TextView tvGraphic = null;
	private TextView tvMainboard = null;
	private TextView tvRam = null;
	private TextView tvHarddisk = null;
	private TextView tvPowersupply = null;
	private TextView tvExtras = null;
	private RatingBar rbCpuBenchmark = null;
	private RatingBar rbGraphicBenchmark = null;
	private FrameLayout pieChart = null;
	private TextView tvComment = null;
	private TextView tvHardwareCosts = null;
	private TextView tvSoftwareCosts = null;
	private TextView tvConsultingCosts = null;
	private TextView tvExtraCosts = null;
	private TextView tvAbTitle = null;
	private TextView tvGalleryLabel = null;

	public static final int DIALOG_OVERVIEW_COSTS = 0;
	public static final int DIALOG_PICTURE_FULLSCREEN = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.examplesdetailview);
		initializeReferences();
		initializeActionBar();
		initializeViewElements();

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			int examplesID = extras.getInt("id");
			setDescription(examplesID);
			setPieChart();
			setHardware(examplesID);
			setBenchmark(examplesID);
			setGalleryPictures(examplesID);
		}

	}

	private void initializeReferences() {
		database.getWritableDatabase();
	}

	private void initializeViewElements() {

		tvDate = (TextView) findViewById(R.id.tvExamplesDetailDateText);
		tvDescription = (TextView) findViewById(R.id.tvExamplesDetailDescriptionText);
		tvProcessor = (TextView) findViewById(R.id.tvExamplesDetailProcessor);
		tvGraphic = (TextView) findViewById(R.id.tvExamplesDetailGraphic);
		tvMainboard = (TextView) findViewById(R.id.tvExamplesDetailMainboard);
		tvRam = (TextView) findViewById(R.id.tvExamplesDetailRam);
		tvHarddisk = (TextView) findViewById(R.id.tvExamplesDetailHarddisk);
		tvPowersupply = (TextView) findViewById(R.id.tvExamplesDetailPowersupply);
		tvExtras = (TextView) findViewById(R.id.tvExamplesDetailExtras);
		rbCpuBenchmark = (RatingBar) findViewById(R.id.rbBenchmarkCpu);
		rbGraphicBenchmark = (RatingBar) findViewById(R.id.rbBenchmarkGraphic);
		pieChart = (FrameLayout) findViewById(R.id.flPieChart);
		tvComment = (TextView) findViewById(R.id.tvExamplesDetailCommentText);
		tvHardwareCosts = (TextView) findViewById(R.id.tvHardwareCosts);
		tvSoftwareCosts = (TextView) findViewById(R.id.tvSoftwareCosts);
		tvConsultingCosts = (TextView) findViewById(R.id.tvConsultingCosts);
		tvExtraCosts = (TextView) findViewById(R.id.tvExtraCosts);
		gallery = (Gallery) findViewById(R.id.gallery);
		tvGalleryLabel = (TextView) findViewById(R.id.tvExamplesDetailPicturesHeader);
	}

	private void initializeActionBar() {

		ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		
		actionBar.setHomeButtonEnabled(true);
		actionBar.setDisplayShowCustomEnabled(true);
		actionBar.setDisplayUseLogoEnabled(true);
		actionBar.setCustomView(R.layout.ab_examples_detail);
		tvAbTitle = (TextView) findViewById(R.id.tvTitle);
		tvAbTitle.setText(getString(R.string.app_ab_profilesdetail));

	}

	
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			
			return true;
		
		}
		return super.onOptionsItemSelected(item);
	}

	private void setDescription(int id) {

		exampleDetailCursor = database.getExample(id);

		try {

			while (exampleDetailCursor.moveToNext()) {

				date = exampleDetailCursor.getString(2);
				String description = exampleDetailCursor.getString(5);
				String comment = exampleDetailCursor.getString(6);
				hardwareCosts = exampleDetailCursor.getString(7);
				softwareCosts = exampleDetailCursor.getString(8);
				consultingCosts = exampleDetailCursor.getString(9);
				extracosts = exampleDetailCursor.getString(10);
				cpuBenchmark = exampleDetailCursor.getString(12);
				graphicBenchmark = exampleDetailCursor.getString(13);
				tvDate.setText(date);
				tvDescription.setText(description);
				if (!comment.equals("")) {
					tvComment.setText(comment);
				}
				tvHardwareCosts.setText(" - " + hardwareCosts + " Euro");
				tvSoftwareCosts.setText(" - " + softwareCosts + " Euro");
				tvConsultingCosts.setText(" - " + consultingCosts + " Euro");
				tvExtraCosts.setText(" - " + extracosts + " Euro");

			}
		} catch (SQLiteException e) {
			e.printStackTrace();
		} finally {
			exampleDetailCursor.close();
		}

	}

	private void setPieChart() {
		Log.d(TAG, "w: " + pieChart.getWidth() + " h:" + pieChart.getHeight());
		double faktor = 0;
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		switch (metrics.densityDpi) {
		case DisplayMetrics.DENSITY_LOW:
			faktor = 0.9;
			break;
		case DisplayMetrics.DENSITY_MEDIUM:
			faktor = 0.7;
			break;
		case DisplayMetrics.DENSITY_HIGH:
			faktor = 0.6;
			break;
		case DisplayMetrics.DENSITY_XHIGH:
			faktor = 0.55;
			break;
		}
		Log.i(TAG, "Faktor: " + faktor);
		DiagramCanvas pieCanvas = new DiagramCanvas(this,
				Float.valueOf(hardwareCosts), Float.valueOf(softwareCosts),
				Float.valueOf(consultingCosts), Float.valueOf(extracosts),
				pieChart.getWidth(), pieChart.getHeight(), faktor);

		pieChart.addView(pieCanvas);

	}

	private void setHardware(int id) {

		exampleDetailCursor = database.getHardwareToExamples(id);
		int hardwareID;
		int hardwareIDs[] = new int[20];
		int i = 0;
		int count = exampleDetailCursor.getCount();

		try {

			while (exampleDetailCursor.moveToNext()) {

				hardwareID = exampleDetailCursor.getInt(1);
				hardwareIDs[i] = hardwareID;
				i++;
			}

			int m = 0;
		} catch (SQLiteException e) {
			e.printStackTrace();
		} finally {
			exampleDetailCursor.close();
		}
		try {

			int m = 0;
			for (int l = 1; l <= count; l++) {
				hardwareCursor = database.getHardware(hardwareIDs[m]);
				hardwareCursor.moveToNext();
				String hardware = hardwareCursor.getString(1);
				String hardwareType = hardwareCursor.getString(2);

				if (hardwareType.equals("PROCESSOR")) {
					tvProcessor.setText(hardware);
				} else if (hardwareType.equals("MAINBOARD")) {
					tvMainboard.setText(hardware);
				} else if (hardwareType.equals("GRAPHIC")) {
					tvGraphic.setText(hardware);
				} else if (hardwareType.equals("RAM")) {
					tvRam.setText(hardware);
				} else if (hardwareType.equals("HARDDISK")) {
					if (tvHarddisk.getText().equals("")) {
						tvHarddisk.setText(hardware);
					} else {
						tvHarddisk.setText(tvHarddisk.getText() + ", "
								+ hardware);
					}
				} else if (hardwareType.equals("POWERSUPPLY")) {
					tvPowersupply.setText(hardware);
				} else if (hardwareType.equals("EXTRAS")) {
					if (tvExtras.getText().equals("Kein Zubehör")) {
						tvExtras.setText(hardware);
					} else {
						tvExtras.setText(tvExtras.getText() + ", " + hardware);
					}
				}
				m++;
			}
		} catch (SQLiteException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			e.printStackTrace();
		} finally {
			hardwareCursor.close();

		}

	}

	private void setBenchmark(int id) {

		rbCpuBenchmark.setRating(Float.valueOf(cpuBenchmark));
		rbGraphicBenchmark.setRating(Float.valueOf(graphicBenchmark));
	}

	private void setGalleryPictures(int id) {

		galleryPicturesCursor = database.getGalleryPictures(id);
		int count = galleryPicturesCursor.getCount();
		final String ids [] = new String [count];
		int i = 0;
		Log.i(TAG, "COunt:  " + count);
		if (count == 0) {
			gallery.setVisibility(View.GONE);
			tvGalleryLabel.setVisibility(View.GONE);
		}

		while (galleryPicturesCursor.moveToNext()) {
			String pictureURL = galleryPicturesCursor.getString(2);
			ids[i] = pictureURL;
			Log.i(TAG, "Pictureurl: " + pictureURL);
			i++;
		}
		
		gallery.setAdapter(new ImageAdapter(this, count, ids));

		gallery.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView parent, View v, int position,
					long id) {
			Bundle args = new Bundle();
			args.putString(BUNDLE_KEY_BIG_PICTURE, ids[position]);
			BigPictureDialog bigPictureDialog = BigPictureDialog.newInstance(args);
			bigPictureDialog.show(getSupportFragmentManager(), TAG_BIG_PICTURE_DIALOG);
			}
		});

	}


	

}
